package com.psd.paomo.wxapi;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import androidx.appcompat.app.AppCompatActivity;
import cc.orange.BuildConfig;
import cc.orange.R;
import cc.orange.entity.WxInfoEntity;
import cc.orange.entity.WxTokenEntity;
import cc.orange.utils.ZXToastUtil;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxentry);

        // 通过 WXAPIFactory 工厂，获取 IWXAPI 的实例
        api = WXAPIFactory.createWXAPI(this, BuildConfig.WX_AppId, true);
        api.handleIntent(getIntent(), this);
//        finish();
        Log.v("---->","asds1");
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {

        Log.v("---->222",""+baseResp.errCode);
        if(baseResp.errCode == 0){
            String code = ((SendAuth.Resp) baseResp).code;
            Log.v("---->333",""+code);
            getToKens(code);
        }else{
            finish();
        }
    }

    private void getToKens(String code) {
        RequestParams rp = new RequestParams("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+BuildConfig.WX_AppId+"&secret="+BuildConfig.WX_AppSecret+"&code="+code+"&grant_type=authorization_code");
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                {
//                    "access_token": "ACCESS_TOKEN",
//                        "expires_in": 7200,
//                        "refresh_token": "REFRESH_TOKEN",
//                        "openid": "OPENID",
//                        "scope": "SCOPE",
//                        "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
//                }
                Log.v("---->access_token1-",""+result);
                Gson gonss = new Gson();
                WxTokenEntity wxTokenEntity = gonss.fromJson(result, WxTokenEntity.class);
                getInfos(wxTokenEntity.getAccessToken(),wxTokenEntity.getOpenid());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ZXToastUtil.showToast("获取token失败");

                Log.v("---->444",""+ex.toString());
                finish();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.v("---->555",""+cex.toString());
            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void getInfos(String tokens,String openId){

        RequestParams rp = new RequestParams("https://api.weixin.qq.com/sns/userinfo?openid="+openId+"&access_token="+tokens);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("---->ac_userinfos-",""+result);
                Gson gonss = new Gson();
                WxInfoEntity wxInfoEntity = gonss.fromJson(result, WxInfoEntity.class);
//                wxInfoEntity.getSex() // 0为男
                EventBus.getDefault().post(wxInfoEntity);
                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ZXToastUtil.showToast("获取用户信息失败");
                finish();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}